/**
 * Created by Zzhk on 2017/6/30.
 */
$(function () {
    var fn = ABUS.Fn;
    var listData = [];

    $('#sorting').on('touchend', function (e) {
        var $modal = $('.sorting');
        if ($modal.is(":visible")) {
            $modal.hide();
        }
        else {
            $modal.show();
        }
        e.stopPropagation();
    });
    $('body').on('touchend', function () {
        $('.sorting').hide();
    });

    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
        isDrag = true;
    }, {passive: false});

    fn.get({
        url: "/user/order/historyChildOrders",
        data: {
            "searchValue": ""
        },
        success: function (data) {
            if (data.result === 1) {
                listData = data.data;
                $('#history_list').renderTemp(data);
                new IScroll('#history_list', {mouseWheel: true});

            } else {
                fn.alert({
                    type: "error",
                    title: data.message
                })
            }
        }
    });

    $("#history_list").on("touchend", "li", function () {
        if (isDrag) {
            isDrag = false;
            return true;
        }
        var data = listData[$(this).index()];
        fn.page_goto({
            url: "./yewuyuan_wode_historyDetails.html",
            data : data
        })
    });

    $("#sorting_list").on("touchend", "li", function () {
        $(this).addClass("current").siblings("li").removeClass("current");
        init($(".search_input input").val())
    });

    $('.search_input input').on('input', _.debounce(function (e) {
        var str = $(this).val();
        init(str)
    },500));

    init("");
    function init(val) {
        fn.get({
            url: "/user/order/historyChildOrders",
            data: {
                "searchValue": val,
                "orderBy": $('#sorting_list li.current').data("value"),//1业务类型 2剩余时间 3开始时间
                "orderType": ""
            },
            success: function (data) {
                $("#history_list").renderTemp(data);
                new IScroll('#history_list', {mouseWheel: true});
            }
        });
    }
});